import { app, BrowserWindow, session } from 'electron';
import path                            from 'path';

// const __dirname = path.resolve();
const reactDevToolsPath = path.resolve(__dirname, '../vue-devtools');

function createWindow() {
  console.log(__dirname);
  console.log('node_env', process.env.NODE_ENV);
  const win = new BrowserWindow({
    width: 1400,
    height: 1000,
    transparent: false,
    backgroundColor: '#ff000000',

    webPreferences: {
      // webSecurity: false,
      defaultEncoding: 'utf-8',
      preload: path.join(__dirname, 'preload.js'),
    },
  });

  let url = process.env.NODE_ENV === 'development' ?
    'http://localhost:80' :
    'file://' + path.join(__dirname, '../dist/index.html');
  win.loadURL(url);
}

app.whenReady().then(async () => {
  if (process.env.NODE_ENV === 'development') {
    await session.defaultSession.loadExtension(reactDevToolsPath);
  }

  createWindow();

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});


